/**
 * 
 */
package valfazz.android.karfinder.activity;

import java.util.ArrayList;

import valfazz.android.karfinder.R;
import valfazz.android.karfinder.database.DatabaseHelper;
import valfazz.android.karfinder.pojo.Playlist;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Nghi
 *
 */
public class PlaylistActivity extends Activity{
	
	
	private DatabaseHelper dbHelper;
	private ListView listView;
	private ArrayList<Playlist> playlistData;
	private PlaylistAdapter playlistAdapter;	
	//private LayoutInflater mInflater;
	//private ArrayList<Integer> arrayChecks;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_playlist);		
		dbHelper = new DatabaseHelper(this);
		//arrayChecks = new ArrayList<Integer>();
		playlistData = dbHelper.fetchPlaylistList();		
		listView = (ListView)findViewById(R.id.lvPlaylist);	
		playlistAdapter = new PlaylistAdapter();		
		listView.setAdapter(playlistAdapter);
		
		//listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		registerForContextMenu(listView);	
		listView.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {				
				Intent intent = new Intent(PlaylistActivity.this, PlaylistViewerActivity.class);
				intent.putExtra("playlistId", playlistData.get(position).getId());
				intent.putExtra("playlistName", playlistData.get(position).getName());
				startActivity(intent);
		//		arrayChecks.add(position);
			}
			
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		dbHelper = new DatabaseHelper(this);
		resetList();
	}

	@Override
	protected void onPause() {		
		super.onPause();
		dbHelper.close();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if(v.getId()==R.id.lvPlaylist){
			// AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			menu.setHeaderTitle("Playlist");
			String[] menuItems = getResources().getStringArray(R.array.contextmenu_playlist);
			for(int i=0; i<menuItems.length; i++){
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}		
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		//return super.onContextItemSelected(item);
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		String[] menuItems = getResources().getStringArray(R.array.contextmenu_playlist);
		String menuItemName = menuItems[menuItemIndex];
		
		//dbHelper = new DatabaseHelper(this);
		final Playlist playlist = new Playlist();
		playlist.setId(playlistData.get(info.position).getId());
		playlist.setName(playlistData.get(info.position).getName());
		
		if(menuItemName == menuItems[0]){			
			
			final EditText etNamePlaylist = new EditText(PlaylistActivity.this);
			etNamePlaylist.setSingleLine(true);
			etNamePlaylist.setText(playlist.getName());
			AlertDialog.Builder dialogEditPlaylist = new AlertDialog.Builder(PlaylistActivity.this);
			dialogEditPlaylist.setTitle(R.string.dialog_edit_playlist);
			dialogEditPlaylist.setView(etNamePlaylist);
			dialogEditPlaylist.setPositiveButton(R.string.dialog_button_playlist_ok, 
								new DialogInterface.OnClickListener() {											
									public void onClick(DialogInterface dialog, int which) {																			
										playlist.setName(etNamePlaylist.getText().toString().trim());
										if(etNamePlaylist.getText().toString().trim().length()==0){
											Toast.makeText(PlaylistActivity.this, R.string.dialog_edit_playlist_fail, Toast.LENGTH_SHORT).show();
										}else{												
											dbHelper.updatePlaylist(playlist);
											Toast.makeText(PlaylistActivity.this, R.string.dialog_edit_playlist_successful, Toast.LENGTH_SHORT).show();
											resetList();
										}																									
									}
								});
			dialogEditPlaylist.setNegativeButton(R.string.dialog_button_playlist_cancel, 
					new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {									
						}
					});
			dialogEditPlaylist.show();	
		}
		if(menuItemName == menuItems[1]){
			dbHelper.deletePlaylist(playlist);
			Toast.makeText(PlaylistActivity.this, R.string.dialog_delete_playlist_successful, Toast.LENGTH_SHORT).show();
			resetList();
		}
		
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		(new MenuInflater(getApplication())).inflate(R.menu.menu_playlist, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return (applyMenuChoice(item)||super.onOptionsItemSelected(item));
	}
	
	private boolean applyMenuChoice(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.menu_playlist_add:
				final EditText etNamePlaylist = new EditText(PlaylistActivity.this);
				etNamePlaylist.setSingleLine(true);
				AlertDialog.Builder dialogAddPlaylist = new AlertDialog.Builder(PlaylistActivity.this);
				dialogAddPlaylist.setTitle(R.string.dialog_add_playlist);
				dialogAddPlaylist.setView(etNamePlaylist);
				dialogAddPlaylist.setPositiveButton(R.string.dialog_button_playlist_ok, 
									new DialogInterface.OnClickListener() {											
										public void onClick(DialogInterface dialog, int which) {
											Playlist playlist = new Playlist();
											playlist.setName(etNamePlaylist.getText().toString().trim());											
											if(playlist.getName().length() == 0){
												Toast.makeText(PlaylistActivity.this, R.string.dialog_add_playlist_fail, Toast.LENGTH_SHORT).show();
											}else{
												dbHelper.addPlaylist(playlist);
												Toast.makeText(PlaylistActivity.this, R.string.dialog_add_playlist_successful, Toast.LENGTH_SHORT).show();
												resetList();
											}																									
										}
									});
				dialogAddPlaylist.setNegativeButton(R.string.dialog_button_playlist_cancel, 
						new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {									
							}
						});
				dialogAddPlaylist.show();	
				break;
			case R.id.menu_playlist_delete:	
				Intent intent = new Intent(PlaylistActivity.this, PlaylistMultiDeleteActivity.class);				
				startActivity(intent);
				/*
				int numberChecks = arrayChecks.size();
				int numberDeletes = 0;				
				Toast.makeText(PlaylistActivity.this, "seleted delete " + numberChecks, Toast.LENGTH_SHORT).show();
				
				if((numberChecks != 0) && (numberChecks == numberDeletes)){					
					Toast.makeText(PlaylistActivity.this, "Deleted all playlist checked", Toast.LENGTH_SHORT).show();
					playlistData = dbHelper.fetchPlaylistList();
					playlistAdapter.notifyDataSetChanged();
				}				
				*/
				break;
		}
		
		return false;
	}
	
	public void resetList(){
		playlistData = dbHelper.fetchPlaylistList();
		playlistAdapter.notifyDataSetChanged();
	}	

	public class PlaylistAdapter extends ArrayAdapter<Playlist> {		
		public PlaylistAdapter() {
			//super(PlaylistActivity.this,R.layout.playlist_item, playlistData);			
			super(PlaylistActivity.this,android.R.layout.simple_list_item_2, playlistData);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			/*
			View row;
			mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (null == convertView) {
    			row = mInflater.inflate(android.R.layout.simple_list_item_2, null);
    		} else {
    			row = convertView;
    		}			
			//TextView playlistName = (TextView)row.findViewById(R.id.tvNamePlaylistList);			
			playlistName.setPadding(10, 10, 10, 10);
			playlistName.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP,20);
			playlistName.setText(playlistData.get(position).getName());			
			//CheckBox cbPlaylist = (CheckBox)row.findViewById(R.id.cbPlaylist);
			//cbPlaylist.setChecked(false);								
			return row;
			*/
			TextView playlistName = new TextView(PlaylistActivity.this);			
			playlistName.setPadding(10, 10, 10, 10);
			playlistName.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP,20);
			playlistName.setText(playlistData.get(position).getName());
			return playlistName;
		}

	}
	
}